package com.example.newproject_overseas.Screen.Main


import android.util.Log
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.example.newproject_overseas.ViewModel.GetOrderViewModel
import com.example.newproject_overseas.ViewModel.OrderDetailsViewModel
import com.example.newproject_overseas.components.CategoryTabs
import com.example.newproject_overseas.components.OrderItems
import com.example.newproject_overseas.components.TopNoBackApp
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch


@Composable
fun OrderDetails(
    navController: NavHostController,
    orderDetailVm: OrderDetailsViewModel = viewModel(),
    GetOrder: GetOrderViewModel = viewModel() //接口
) {
    //页面一上来就调接口
    LaunchedEffect(Unit){
        GetOrder.OrderServicesReq(navController, newIndex = 0)
    }

    var selectIndex by remember {
        mutableStateOf(0)
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(color = Color(0xFFF1F1F1)),

    ) {

        TopNoBackApp(title = "My loan")

//        Column(
//            modifier = Modifier
//                .fillMaxSize(),
//            verticalArrangement = Arrangement.Center,
//            horizontalAlignment = Alignment.CenterHorizontally
//        ) {
//            Image(
//                painter = painterResource(id = R.drawable.empty),
//                contentDescription = null,
//                modifier = Modifier
//
//                    .width(300.dp)
//                    .height(300.dp)
//                    .scale(0.9f)
//            )
//        }


        //分类
        CategoryTabs(
            categories = orderDetailVm.categories,
            selectIndex = selectIndex,
            onTabSelected = { newIndex ->
                GlobalScope.launch {
                    Log.i("-------", newIndex.toString())
                    GetOrder.OrderServicesReq(navController,newIndex)
                    selectIndex = newIndex
                }

            }
        ){ index, category ->

            LazyColumn(){

                //列表数据
                items(orderDetailVm.orderList){orderItem->
                    OrderItems(order = orderItem);
                }
            }
        }

    }

}


@Composable
fun Square() {
    Canvas(
        modifier = Modifier
            .size(35.dp)
            .background(Color.White)
    ) {
        val size = Size(size.width, size.height)
        val cornerRadius = 25f

        // Draw a square with a blue color
        drawRoundRect(color = Color(0xFFFFCC00), size = size,cornerRadius = CornerRadius(cornerRadius))
    }
}






